﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScriptCoreLib;
using ScriptCoreLib.Shared.Avalon.Extensions;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace AvalonTycoonMansion.Shared
{
	[Script]
	public class TycoonButton : ISupportsContainer
	{
		public Canvas Container { get; set; }

		public Brush Foreground
		{
			set
			{
				ForegroundTextBox.Foreground = value;
			}
		}

		TextBox BackgroundTextBox;
		TextBox ForegroundTextBox;

		public readonly int Width;
		public readonly int Height;


		public readonly Canvas BackgroundContainer;

		readonly Rectangle
			BackgroundFill,
			BorderLeft,
			BorderTop,
			BorderRight,
			BorderBottom;

		public bool BackgroundVisible
		{
			get
			{
				return BackgroundContainer.Visibility == Visibility.Visible;
			}
			set
			{
				if (value)
					BackgroundContainer.Visibility = Visibility.Visible;
				else
					BackgroundContainer.Visibility = Visibility.Hidden;
			}
		}
		public TycoonButton(int width, int height)
		{
			this.Width = width;
			this.Height = height;

			Container = new Canvas
			{
				Width = width,
				Height = height,
			};

			BackgroundContainer = new Canvas
			{
				Width = width,
				Height = height,
			}.AttachTo(Container);

			BackgroundFill = new Rectangle
			{
				Width = width,
				Height = height,
				Fill = 0xff848484.ToSolidColorBrush()
			}.AttachTo(BackgroundContainer);

			#region borders
			BorderLeft = new Rectangle
			{
				Width = width,
				Height = 1,
				Fill = 0xffA8A8A8.ToSolidColorBrush()
			}.AttachTo(BackgroundContainer);

			BorderTop = new Rectangle
			{
				Width = 1,
				Height = height,
				Fill = 0xffA8A8A8.ToSolidColorBrush()
			}.AttachTo(BackgroundContainer);

			BorderRight = new Rectangle
			{
				Width = 1,
				Height = height,
				Fill = 0xff646464.ToSolidColorBrush()
			}.AttachTo(BackgroundContainer).MoveTo(width - 1, 0);

			BorderBottom = new Rectangle
			{
				Width = width,
				Height = 1,
				Fill = 0xff646464.ToSolidColorBrush()
			}.AttachTo(BackgroundContainer).MoveTo(0, height - 1);
			#endregion

			var ty = (height - 14) / 2;
			if (ty < 0)
				ty = 0;

			BackgroundTextBox = new TextBox
			{
				AcceptsReturn = true,
				Background = Brushes.Transparent,
				BorderThickness = new Thickness(0),
				TextAlignment = TextAlignment.Center,
				FontFamily = new FontFamily("Courier New"),
				//FontWeight = FontWeights.Bold,
				FontSize = 11,
				Width = width - 6,
				Height = height,
				IsReadOnly = true,
			}.AttachTo(Container).MoveTo(3, ty);

			ForegroundTextBox = new TextBox
			{
				AcceptsReturn = true,
				Background = Brushes.Transparent,
				BorderThickness = new Thickness(0),
				TextAlignment = TextAlignment.Center,
				FontFamily = new FontFamily("Courier New"),
				//FontWeight = FontWeights.Bold,
				FontSize = 11,
				Foreground = Brushes.White,
				Height = height,
				Width = width - 6,
				IsReadOnly = true,

			}.AttachTo(Container).MoveTo(2, -1 + ty);

		}

		public bool IsAnimated;

		public TextAlignment TextAlignment
		{
			set
			{
				ForegroundTextBox.TextAlignment = value;
				BackgroundTextBox.TextAlignment = value;
			}
		}
		string InternalText;
		DispatcherTimer TextAnimator;
		public string Text
		{
			get
			{
				return InternalText;
			}
			set
			{
				InternalText = value;

				Action<string> SetText =
					text =>
					{
						ForegroundTextBox.Text = text;
						BackgroundTextBox.Text = text;

						var c = 1;

						foreach (char x in text)
						{
							if (x == '\n')
								c++;
						}

						var ty = (this.Height - 14 * c) / 2;
						if (ty < 0)
							ty = 0;

						BackgroundTextBox.MoveTo(3, ty);
						ForegroundTextBox.MoveTo(2, -1 + ty);
					};


				if (TextAnimator != null)
					TextAnimator.Stop();

				if (IsAnimated)
				{
					var j = 0;
					var i = 0;
					TextAnimator = 50.AtIntervalWithTimer(
						t =>
						{
							if (j > 0)
							{
								j--;
								return;
							}


							if (value[i] == '-')
								j += 10;

							if (value[i] == '.')
								j += 10;

							if (value[i] == '!')
								j += 10;
							if (value[i] == ',')
								j += 10;

							i++;

							if (i < value.Length - 1)
								SetText(value.Substring(0, i) + "_");
							else
								SetText(value.Substring(0, i));

							if (i >= value.Length)
								t.Stop();
						}
					);
				}
				else
				{
					SetText(value);
				}
			}
		}


	}
}
